package cn.dnui_cr503.dnuifood_16110100503.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.FoodBean;
import cn.dnui_cr503.dnuifood_16110100503.controller.FoodActivity;
import cn.dnui_cr503.dnuifood_16110100503.model.UserModel;

/**
 * Created by chengrui on 2018/11/14.
 */

public class FoodListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<FoodBean> mDataList;
    private LayoutInflater mLayoutInflater;

    public FoodListAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.food_item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final FoodBean entity = (FoodBean) mDataList.get(position);
        if (null == entity)
            return;
        FoodListAdapter.ViewHolder viewHolder = (FoodListAdapter.ViewHolder) holder;
        viewHolder.food_name.setText(entity.getFoodname());
        viewHolder.food_price.setText(entity.getPrice()+"元");

        Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(viewHolder.food_image);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = String.valueOf(mDataList.get(position).getFood_id());
                String str1 = String.valueOf(mDataList.get(position).getFoodname());
                Intent intent = new Intent(mContext,FoodActivity.class);
                intent.putExtra("shop_id",entity.getShop_id());
                intent.putExtra("food_id",str);
                intent.putExtra("foodname",str1);
                mContext.startActivity(intent);
            }
        });

    }
    @Override
    public int getItemCount() {
            return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView food_name,food_price;
        ImageView food_image;
        public ViewHolder(View view) {
            super(view);
            food_name=(TextView)view.findViewById(R.id.food_name);
            food_price=(TextView)view.findViewById(R.id.food_price);
            food_image=(ImageView)view.findViewById(R.id.food_image);
        }
    }


}
